<?php

namespace App\Admin\Metrics;

use App\Admin\Forms\SetupOssForm;
use App\Admin\Forms\ZipPasswordForm;
use App\Models\Application;
use Dcat\Admin\Admin;
use Dcat\Admin\Widgets\Metrics\Card;
use Dcat\Admin\Widgets\Modal;
use Throwable;

class ApplicationConfig extends Card
{
    protected ?Application $application = null;

    /**
     * @throws Throwable
     */
    public function renderContent(): string
    {
        $app = Application::where('admin_id', Admin::user()->getKey())->first();

        if (!$app) {
            return '';
        }

        $this->application = $app;

        return <<<HTML
            <div class="p-1">
                <div class="d-flex mb-2" >
                    <div class="d-flex align-items-center">
                        <i class="fa fa-circle-o"></i>
                        <div class="ml-50" style="width: 100px">应用名称：</div>
                        <div>$app->name</div>
                    </div>
                </div>
                <div class="d-flex mb-2" >
                    <div class="d-flex align-items-center">
                        <i class="fa fa-circle-o"></i>
                        <div class="ml-50" style="width: 100px">AppKey：</div>
                        <div>$app->app_key</div>
                    </div>
                </div>
                <div class="d-flex mb-2" >
                    <div class="d-flex align-items-center">
                        <i class="fa fa-circle-o"></i>
                        <div class="ml-50" style="width: 100px">海报解压密码：</div>
                        <div>$app->password</div>
                    </div>
                </div>
                <div class="d-flex mb-2" >
                    <div class="d-flex align-items-center">
                        <i class="fa fa-circle-o"></i>
                        <div class="ml-50" style="width: 100px">到期时间：</div>
                        <div>$app->expired_at</div>
                    </div>
                </div>
                <div class="d-flex mb-2" >
                    <div class="d-flex align-items-center">
                        <i class="fa fa-circle-o"></i>
                        <div class="ml-50" style="width: 100px">CDN(存储)：</div>
                        <div>CDN: {$app->domainPool?->domain}<br/>BUCKET: {$app->domainPool?->objectStorageService?->bucket}</div>
                    </div>
                </div>
            </div>
            {$this->renderFooter()}
            HTML;
    }

    /**
     * @throws Throwable
     */
    public function renderFooter(): string
    {
        return <<<HTML
            <div class="border-top pt-1 pb-1 pr-1 text-right">
                {$this->setPassword()}
                {$this->setOss()}
            </div>
            HTML;
    }

    /**
     * @throws Throwable
     */
    public function setOss()
    {
        return Modal::make()
            ->lg()
            ->centered()
            ->title('设置CDN(存储)')
            ->body(SetupOssForm::make()->payload(['application_id' => $this->application->id]))
            ->button('<button class="btn btn-info">设置CDN(存储)</button>')
            ->render();
    }

    protected function setPassword(): string
    {
        return Modal::make()
            ->centered()
            ->title('设置解压密码')
            ->body(ZipPasswordForm::make())
            ->button('<button class="btn btn-primary">设置解压密码</button>');
    }

    protected function init(): void
    {
        parent::init();
        $this->title = '应用信息';
    }
}
